<?php
//模型
class UserAppointModel extends MY_Model {
	function __construct(){
		parent::__construct();
		$this->table = 'user_appoint';
	}
	//分页列表
	function get_list($arr=array(),$page=1,$size=10){
		$where = " where 1 ";
		if($arr['user_id']){
			$where .= " and a.user_id='{$arr['user_id']}' ";
		}		
		if(isset($arr['type'])){
			if($arr['type']!==null){			
				$where .= " and a.`status`= '{$arr['type']}' ";
			}
		}else{
			$where .= " and a.`status` <> 2 ";
		}
		
		$sql  = "select a.id,a.mid, a.user_id, a.cid, a.sdate, a.stime, a.status, c.type_id, c.name, c.theme_img, c.duration, s.name as shop_name from 
				".$this->db->dbprefix.$this->table." as a left join 
				".$this->db->dbprefix."course as c on a.cid=c.id left join 
				
				".$this->db->dbprefix."shop as s on a.shop_id=s.id {$where} order by a.sdate desc LIMIT ".($page-1)*$size .",{$size}";
		$query = $this->db->query($sql);
		$res = $query->result_array();		
		
		$time = '  ';
		foreach($res as $k=>$v){
			$res[$k]['atime'] = '';
			if($v['sdate'] && $v['stime'] && $v['duration']){
				$datetime = strtotime($v['sdate'].' '.$v['stime'].':00')+$v['duration']*60;
				$res[$k]['atime'] = date("H:i",$datetime);
			}
			//是否取消预约			
			$res[$k]['is_cancel'] = 0;
			if($v['sdate'] && $v['stime'] && $v['status']==0){
				$start_time = $v['sdate'].' '.$v['stime'].':00';
				$start_time = strtotime($start_time);
				if($start_time - $time <= 2*60*60){
					//$this->err_out('开始前两小时无法取消预约');
					$res[$k]['is_cancel'] = 1;
				}
			}
			
		}
		
		$sql  = "select count(*) as num from ".$this->db->dbprefix.$this->table." as a ".$where;
		$query = $this->db->query($sql);
		$row = $query->row_array();
		return array('list'=>$res,'count' =>$row['num']);		
	}
	//全部列表
	function get_lists($arr=array()){
		$where = " where 1 ";
		if($arr['user_id']){
			$where .= " and a.user_id='{$arr['user_id']}' ";
		}
		
		$sql  = "select a.id, a.user_id, a.cid, a.sdate, a.stime, a.status, c.type_id, c.name, c.theme_img, c.duration, s.name as shop_name from 
			".$this->db->dbprefix.$this->table." as a left join 
			".$this->db->dbprefix."course as c on a.cid=c.id left join 
			".$this->db->dbprefix."shop as s on a.shop_id=s.id 
			".$where;
		$query = $this->db->query($sql);
		$res = $query->result_array();
		
		foreach($res as $k=>$v){
			$res[$k]['atime'] = '';
			if($v['sdate'] && $v['stime'] && $v['duration']){
				$datetime = strtotime($v['sdate'].' '.$v['stime'].':00')+$v['duration']*60;
				$res[$k]['atime'] = date("H:i",$datetime);
			}
			
			//是否取消预约			
			$res[$k]['is_cancel'] = 0;
			if($v['sdate'] && $v['stime'] && $v['status']==0){
				$start_time = $v['sdate'].' '.$v['stime'].':00';
				$start_time = strtotime($start_time);
				if($start_time - $time <= 2*60*60){
					//$this->err_out('开始前两小时无法取消预约');
					$res[$k]['is_cancel'] = 1;
				}
			}
		}
		
		return $res;		
	}
	//详情
	function get_info($id){		
		$sql  = "select a.id, a.user_id, a.cid, a.card_id, a.sdate, a.stime, a.status, c.type_id, c.name, c.theme_img, c.duration, s.name as shop_name from 
		".$this->db->dbprefix.$this->table." as a left join 
		".$this->db->dbprefix."course as c on a.cid=c.id left join 
		".$this->db->dbprefix."shop as s on a.shop_id=s.id 
		where a.id='{$id}' ";
		$query = $this->db->query($sql);
		$row = $query->row_array();
		
		$row['atime'] = '';
		if($row['sdate'] && $row['stime'] && $row['duration']){
			$datetime = strtotime($row['sdate'].' '.$row['stime'].':00')+$row['duration']*60;
			$row['atime'] = date("H:i",$datetime);
		}
		
		
		return $row;
	}
	//唯一条件获取详情
	function unique_get_info($field,$value){
		$sql  = "select * from ".$this->db->dbprefix.$this->table." where {$field}='{$value}' ";
		$query = $this->db->query($sql);
		$row = $query->row_array();		
		return $row;
	}
	//获取课程总人数
	function get_join_num($date,$time,$cid){
		$sql  = "select count(*) as num from ".$this->db->dbprefix.$this->table." where sdate='{$date}' and stime='{$time}' and cid='{$cid}' and status <>2 ";
		$query = $this->db->query($sql);
		$row = $query->row_array();
		return $row['num']?$row['num']:0;
	}
	//检查重复预约
	function get_join_repeat($date,$time,$cid,$user_id){
		$sql  = "select id from ".$this->db->dbprefix.$this->table." where sdate='{$date}' and stime='{$time}' and cid='{$cid}' and user_id='{$user_id}' and status <>2";
		$query = $this->db->query($sql);
		$row = $query->row_array();
		return $row;
	}
	
}

